﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MeetFuture.MRC
{
	public partial class clsBatchInfo: ICloneable
	{
        private const string key_split = ",";
        private const string key_BatchID = "BATCHID=";
        private const string key_orderIndex = "OrderIndex=";
        private const string key_AGV = "AGV=";

        private int _OrderIndex = -1;
        private string _TransId = string.Empty;
        private string _BatchID = string.Empty;
        private int _BatchSize = 0;
        private string _AGV = string.Empty;
        private string _EQP = string.Empty;
        private string _PPID = string.Empty;
        private List<clsLotInfo> _Lots = new List<clsLotInfo>();

        private DateTime _StartTime = DateTime.MinValue;
        private DateTime _EndTime = DateTime.MinValue;

        public int OrderIndex
        {
            get { return this._OrderIndex; }
            set 
            { 
                this._OrderIndex = value; 
                foreach (clsLotInfo lot in this._Lots)
                {
                    lot.OrderIndex = this._OrderIndex;
                }
            }
        }
        public string TransID
        {
            get { return this._TransId; }
            set 
            { 
                this._TransId = value;
                foreach (clsLotInfo lot in this._Lots)
                {
                    lot.TransID = this._TransId;
                }
            }
        }
        public string BatchID
        {
            get { return this._BatchID; }
            set 
            { 
                this._BatchID = value; 
                foreach (clsLotInfo lot in this._Lots)
                {
                    lot.Batch = this._BatchID;
                }
            }
        }
        public int BatchSize
        {
            get { return this._BatchSize; }
            set 
            { 
                this._BatchSize = value;
                foreach (clsLotInfo lot in this._Lots)
                {
                    lot.BatchSize = this._BatchSize.ToString();
                }
            }
        }
        public string AGV
        {
            get 
            {
                foreach (clsLotInfo lot in this._Lots)
                {
                    if (!string.IsNullOrEmpty(lot.AGV))
                    {
                        this._AGV = lot.AGV;
                        break;
                    }                    
                }
                return this._AGV; 
            }
            set 
            { 
                this._AGV = value;
                foreach (clsLotInfo lot in this._Lots)
                {
                    lot.AGV = this._AGV;
                }
            }
        }
        public string EQP
        {
            get { return this._EQP; }
            set 
            { 
                this._EQP = value;
                foreach (clsLotInfo lot in this._Lots)
                {
                    lot.EQP = this._EQP;
                }
            }
        }
        public string PPID
        {
            get { return this._PPID; }
            set 
            { 
                this._PPID = value;
                foreach (clsLotInfo lot in this._Lots)
                {
                    lot.PPID = this._PPID;
                }
            }
        }
        public string Pri
        {
            get
            {
                string result = string.Empty;
                try
                {
                    foreach (clsLotInfo lotinfo in this._Lots)
                    {
                        if (string.IsNullOrEmpty(result))
                        {
                            result = lotinfo.Pri;
                        }
                        else
                        {
                            int int_result = Convert.ToInt32(result);
                            int int_lotPri = Convert.ToInt32(lotinfo.Pri);
                            if (int_lotPri < int_result)
                            {
                                result = lotinfo.Pri;
                            }
                        }
                    }
                }
                catch (System.Exception ex)
                {
                }
                return result;
            }
        }
        public DateTime StartTime
        {
            get { return this._StartTime; }
            set { this._StartTime = value; }
        }
        public DateTime EndTime
        {
            get { return this._EndTime; }
            set { this._EndTime = value; }
        }
        public DateTime Created { get; private set; } = default(DateTime);
        public List<clsLotInfo> Lots
        {
            get { return this._Lots; }
            set { this._Lots = value; }
        }
        public bool IsReady
        {
            get 
            {
                bool result = false;
                try
                {
                	foreach (clsLotInfo lot in this._Lots)
                	{
                	    if (lot.IsJobPrepare && lot.IsOnRack)
                	    {
                            result = true;

                            if(Created == default(DateTime)) Created = DateTime.Now;
                        }
                        else if (lot.IsUnlocked)
                        {
                            result = true;

                            if (Created == default(DateTime)) Created = DateTime.Now;
                        }
                        else
                        {
                            result = false;
                            break;
                        }
                	}
                }
                catch (System.Exception ex)
                {	
                }
                return result;
            }
        }

        public clsBatchInfo()
        {
            this._OrderIndex = -1;
            this._TransId = string.Empty;
            this._BatchID = string.Empty;
            this._BatchSize = 0;
            this._AGV = string.Empty;
            this._EQP = string.Empty;
            this._PPID = string.Empty;
            this._Lots = new List<clsLotInfo>();
        }

        public clsLotInfo FindLotByLotID(string LotID)
        {
            clsLotInfo result = null;
            try
            {
            	foreach (clsLotInfo lot in this._Lots)
            	{
            	    if (lot.LotID == LotID)
            	    {
                        result = lot;
                        break;
            	    }
            	}
            }
            catch (System.Exception ex)
            {
                result = null;
            }
            return result;
        }

        public object Clone() => MemberwiseClone();
    }
}
